package proxy.wrappers;

import android.content.pm.PackageInfo;
import android.os.IInterface;
import com.genymobile.scrcpy.Ln;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes2.dex */
public final class PackageManager {
    private Method getAllPackagesMethod;
    private Method getPackageInfoMethod;
    private final IInterface manager;

    public PackageManager(IInterface manager) {
        this.manager = manager;
    }

    private Method getAllPackagesMethod() throws NoSuchMethodException {
        if (this.getAllPackagesMethod == null) {
            this.getAllPackagesMethod = this.manager.getClass().getMethod("getAllPackages", new Class[0]);
        }
        return this.getAllPackagesMethod;
    }

    private Method getPackageInfoMethod() throws NoSuchMethodException {
        if (this.getPackageInfoMethod == null) {
            this.getPackageInfoMethod = this.manager.getClass().getMethod("getPackageInfo", String.class, Integer.TYPE, Integer.TYPE);
        }
        return this.getPackageInfoMethod;
    }

    public List<String> getAllPackages() {
        List<String> pkgList = new ArrayList<>();
        try {
            Method method = getAllPackagesMethod();
            List<String> pkgList2 = (List) method.invoke(this.manager, new Object[0]);
            return pkgList2;
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            Ln.e("Could not invoke method", e);
            return pkgList;
        }
    }

    public List<PackageInfo> getInstalledPackages() {
        List<PackageInfo> packageInfos = new ArrayList<>();
        try {
            List<String> allPackages = getAllPackages();
            Method pkgInfoMethod = getPackageInfoMethod();
            for (int i = 0; i < allPackages.size(); i++) {
                String pkg = allPackages.get(i);
                PackageInfo pkgInfo = (PackageInfo) pkgInfoMethod.invoke(this.manager, pkg, 0, 0);
                if (pkgInfo != null && (pkgInfo.applicationInfo.flags & 1) == 0) {
                    packageInfos.add(pkgInfo);
                }
            }
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            Ln.e("Could not invoke method", e);
        }
        return packageInfos;
    }
}